﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Dapper;
using log4net;

namespace Common.Extension
{
    public static class DapperExtension
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(DapperExtension));
        //dapper在事务中批量insert
        public static int InsertMultiple(this IDbConnection cnn, string sql, object param = null)
        {
            using (var trans = cnn.BeginTransaction())
            {
                try
                {
                    int size =  cnn.Execute(sql, param, trans);
                    trans.Commit();
                    return size;
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    trans.Rollback();
                    return 0;
                    //throw e;
                }
            }
        }
    }
}